package crud

import (
	"os"
	"strings"

	"api/system/helper"
)

var cServiceFacadeTempl = `<?php
namespace {{.__NAMESPACE__}};

use think\Facade;

/**
 * 服务门面
 * @package app\api\service
 * @method static mixed getError()             获取错误信息
 * @method static bool doSave(array $param)             增加 和 编辑 合二为一
 * @method static array info(int $id)             获取信息
 * @method static array list(array $param)             获取列表
 * @method static bool defaults(int $id)             设为默认值
 * @method static bool delete(int $id)             删除数据
 */
class {{.__CLASS__}}Facade extends Facade
{
    public static function getFacadeClass() {
        return {{.__CLASS__}}::class;
    }
}`

// 生成控制器
func (g *cGlobal) MakeServiceFacade() error {
	inx := strings.LastIndex(g.ServicePath, "\\")
	nName := g.ServicePath[:inx]
	err := os.MkdirAll(nName, os.ModePerm)
	if err != nil {
		return err
	}

	className := ""
	cinx := strings.LastIndex(g.TableName, "_")
	if cinx > 0 {
		lowerClassName := g.TableName[cinx+1:]
		className = helper.UcFirst(lowerClassName)
	} else {
		className = helper.UcFirst(g.TableName)
	}

	m := map[string]string{
		"__NAMESPACE__": g.ServiceNameSpace,
		"__CLASS__":     className,
	}

	fileName := g.ServicePath
	fileName = strings.ReplaceAll(fileName, className+".php", className+"Facade.php")
	err = TemplateExec(fileName, cServiceFacadeTempl, m)
	if err != nil {
		return err
	}

	return nil
}
